import requests
import base64
import hmac
import hashlib


def build_canonical_string(method, expires):
    """构造规范请求字符串"""
    bucket = "winrobot-pri-a"
    key = "robots/robot-d0d07e72-f3d2-4685-9e4c-bf6e51567608/v-1/package.bot"

    # 构造规范字符串
    canonical_string = f"{method}\n\n\n{expires}\n/{bucket}/{key}"
    return canonical_string


def generate_oss_signature(canonical_string):
    """生成OSS签名"""
    # 使用HMAC-SHA1加密，注意这里用的是 AccessKeySecret
    h = hmac.new(
        "LTAI5t93vfGfysekhtsfrZU9".encode(),
        canonical_string.encode(),
        hashlib.sha1
    )
    # Base64编码
    signature = base64.b64encode(h.digest()).decode()
    return signature



canonical_string = build_canonical_string("PUT", "1764134363")

# 2. 生成签名
signature = generate_oss_signature(canonical_string)
print(f"Generated Signature: {signature}")

# headers = {
#     "Content-Type": "application/octet-stream"
# }
# url = "https://winrobot-pri-a.oss-cn-hangzhou.aliyuncs.com/robots/robot-d0d07e72-f3d2-4685-9e4c-bf6e51567608/v-1/package.bot"
# params = {
#     "Expires": "1764134363",
#     "OSSAccessKeyId": "LTAI5t93vfGfysekhtsfrZU9",
#     "Signature": "ZXGfDaSL5c0CIlhxksWlpHlOfA4="
# }
# data = '@C:\\Users\\15054\\AppData\\Roaming\\Reqable\\tmp\\a998b719-2b62-4248-8b8c-6af083abe111'.encode(
#     'unicode_escape')
# response = requests.put(url, headers=headers, params=params, data=data)
#
# print(response.text)
# print(response)
